<?php
class Trainee extends AppModel {
	var $name = 'Trainee';
	//var $displayField = 'name';
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
		'Country' => array(
			'className' => 'Country',
			'foreignKey' => 'country_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'City' => array(
			'className' => 'City',
			'foreignKey' => 'city_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	var $validate = array(
		'user' => array (
							'rule' => '/^[a-z0-9]{4,40}$/i',
							'message' => 'User name must have between 4 and 40 alphanumeric characters.'
						),
		// 'pass' => array (
							// 'rule' => '/^[a-z0-9]{6,40}$/i',							
							// 'message' => 'Password must have between 6 and 40 alphanumeric characters.'
						// ),
		'email' => array(
							'rule'=>'email', 
							'message'=>'Please supply a valid email address'
						),
		'gender' => array(
							'rule'=>'notEmpty', 
							'message'=>'Gender cannot be left blank'
						),
		'firstname' => array(
							'rule'=>'notEmpty', 
							'message'=>'First name cannot be left blank'
						),
		'lastname' => array(
							'rule'=>'notEmpty', 
							'message'=>'Last name cannot be left blank'
						),
		//2013-02-25 Pham Vien: fixed bug: Frontpage_Register_EF.docx - 1.
		// 'idcard' => array(
							// 'rule'=>'notEmpty', 
							// 'message'=>'IDcard/Passport cannot be left blank'
						// ),	
		'addtyp' => array(
							'rule'=>'notEmpty', 
							'message'=>'Address type cannot be left blank'
						),				
		'address' => array(
							'rule'=>'notEmpty', 
							'message'=>'Address cannot be left blank'
						),		
		'country_id' => array(
							'rule'=>'notEmpty', 
							'message'=>'Country cannot be left blank'
						),		
		'city_id' => array(
							'rule'=>'notEmpty', 
							'message'=>'City cannot be left blank'
						),		
		'phonetyp' => array(
							'rule'=>'notEmpty', 
							'message'=>'Phone Type cannot be left blank'
						),		
		'phone' => array(
							'rule'=>'notEmpty', 
							'message'=>'Phone cannot be left blank'
						),
		'condition' => array(
					// 'required' => true,
					'rule'=>array('comparison', '!=', 0),
					'message'=>'You must accept the Terms & Conditions',
					'on' => 'create'
				),
		);
		
		
	//cac thao tac thuc hien truoc khi validate
	function beforeValidate()
	{
		$result = true;
		if (!$this->id)		//id chua co' => truong hop them moi
		{
			if ($this->findByUser($this->data['Trainee']['user']))
			{
				$this->invalidate('username_unique');
				$result = false;
			}
			if ($this->findByEmail($this->data['Trainee']['email']))
			{
				$this->invalidate('email_unique');
				$result = false;
			}
			//2013-02-25 Pham Vien: fixed bug: Frontpage_Register_EF.docx - 1.
			// if ($this->findByIdcard($this->data['Trainee']['idcard']))
			// {
				// $this->invalidate('idcard_unique');
				// $result = false;
			// }
		}
		return $result;
	}
}
